package controllers;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class AddLinkServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Utils.checkLoginFalse(request, response);
		
		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		if(object!=null)
		{
			DBCollection collection = Data.data("urlCraw");
			DBCursor cursor = collection.find(new BasicDBObject("username",object.get("username").toString()));
			
			request.setAttribute("cursor", cursor);
		}
		String[] listError = new String[5];
		request.setAttribute("listError", listError);
		try {
			request.getRequestDispatcher("/addlink.jsp").forward(request,
					response);
		} catch (Exception e) {
			// new SLogger(LoginServlet.class.getName(), e.toString());
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Utils.checkLoginFalse(request, response);
		
		String sitemap = request.getParameter("sitemap");
		String reglink = request.getParameter("reglink");
		String regtitle = request.getParameter("regtitle");
		String regcontent = request.getParameter("regcontent");
		String regremove = request.getParameter("regremove");
		String type = request.getParameter("type");
		String typeLink = request.getParameter("typeLink");
		String source = request.getParameter("source");
		
		sitemap = Utils.fomatSitemap(sitemap);
		
		String[] listError = new String[5];
		if(sitemap!=null&&sitemap.equals(""))
			listError[0]="Sitemap is not empty";
		if(reglink!=null&&reglink.equals(""))
			listError[1]="Regexp get link is not empty";
		if(regtitle!=null&&regtitle.equals(""))
			listError[2]="Regexp title  is not empty";
		if(regcontent!=null&&regcontent.equals(""))
			listError[3]="Regexp content is not empty";
		DBCollection collection = Data.data("urlCraw");
		
		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		if(listError[0]==null&&listError[1]==null&&listError[3]==null&&listError[2]==null)
		{
			DBObject dbObject = collection.findOne(new BasicDBObject("sitemap",sitemap).append("username", object.get("username")));
			if(dbObject==null)
			{
				BasicDBObject basicDBObject = new BasicDBObject();
				basicDBObject.put("username", object.get("username").toString());
				basicDBObject.put("sitemap", sitemap);
				basicDBObject.put("reglink", reglink);
				basicDBObject.put("regtitle", regtitle);
				basicDBObject.put("regcontent", regcontent);
				basicDBObject.put("regremove", regremove);
				basicDBObject.put("domain", Utils.fomatDmain(sitemap));
				basicDBObject.put("type", type);
				basicDBObject.put("typeLink", typeLink);
				basicDBObject.put("source", source);
				basicDBObject.put("time", new Date().getTime());
				
				collection.insert(basicDBObject);
				request.setAttribute("success", "Add link success");
			} else {
				listError[4] = "Sitemaps already exists";
				request.setAttribute("sitemap", sitemap);
				request.setAttribute("reglink", reglink);
				request.setAttribute("regtitle", regtitle);
				request.setAttribute("regcontent", regcontent);
				request.setAttribute("regremove", regremove);
				request.setAttribute("type", type);
				request.setAttribute("typeLink", typeLink);
				request.setAttribute("source", source);
			}
		} else {
			request.setAttribute("sitemap", sitemap);
			request.setAttribute("reglink", reglink);
			request.setAttribute("regtitle", regtitle);
			request.setAttribute("regcontent", regcontent);
			request.setAttribute("regremove", regremove);
			request.setAttribute("type", type);
			request.setAttribute("typeLink", typeLink);
			request.setAttribute("source", source);
		}
		if(object!=null)
		{
			DBCursor cursor = collection.find(new BasicDBObject("username",object.get("username").toString()));
			
			request.setAttribute("cursor", cursor);
		}
		request.setAttribute("listError", listError);
		try {
			request.getRequestDispatcher("/addlink.jsp").forward(request,
					response);
		} catch (Exception e) {
			// new SLogger(LoginServlet.class.getName(), e.toString());
		}
	}

}
